﻿using Microsoft.Win32;
using System;

namespace SAInterfaceLibrary.MSVCVersions
{
    class MSVC2010 : ConfigMSVCVersionInfo
    {
        public MSVC2010(string template_dir)
        {
            _name = "2010";
            _template_dir = template_dir + @"\2010";
            _template_location = @"vc_project_template.vcxproj";

            AddFileToTranslate(new FileTransInfo() { FinalFileName = "root_build_project.vcxproj.FILTERS", TemplatePath = "vc_project_template.vcxproj.FILTERS" });
        }

        /// <summary>
        /// Return true if we can find the 2010 unique registry entries
        /// </summary>
        override public bool IsOnMachine
        {
            get
            {
                return GetRawInstallDir() != null;
            }
        }

        /// <summary>
        /// See if we can find the directory where devenv should exist.
        /// </summary>
        /// <returns></returns>
        protected override string GetDirectory()
        {
            ///
            /// Make sure that 9.0 is installed on this machine
            /// 

            RegistryKey k10 = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\VisualStudio\10.0");
            if (k10 == null)
            {
                return null;
            }

            ///
            /// Getting the install name is a bit of a pain - we need to use the CLSID to do this
            /// 

            RegistryKey clsid = Registry.ClassesRoot.OpenSubKey(@"VisualStudio.accessor.10.0\shell\Open\Command");
            var devenvString = "devenv.exe";
            if (clsid == null)
            {
                clsid = Registry.ClassesRoot.OpenSubKey(@"VCExpress.sln.10.0\shell\Open\Command");
                if (clsid == null)
                    return null;
                devenvString = "VCExpress.exe";
            }

            string cmd = (string)clsid.GetValue("");
            if (cmd == null || cmd[0] != '"')
            {
                return null;
            }
            cmd = cmd.Substring(1);
            cmd = cmd.Substring(0, cmd.IndexOf(@"\" + devenvString));
            return cmd;
        }

        /// <summary>
        /// We do not want a settings file!
        /// </summary>
        public override bool UsesSettingsFile
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// We will translate the special setup macro here - because beta one of msvc doesn't have
        /// the env variables defined. Hopefully we can remove this for the final version of MSVC!
        /// We base this on special knowledge of the layout of MSVC2010 files... so don't be surprised
        /// if this breaks...
        /// </summary>
        /// <param name="input_string"></param>
        /// <returns></returns>
        public override string TranslateMacro(string input_string)
        {
            if (input_string == "special-command-setup")
            {
                string devenv = GetDirectory();
                if (devenv == null)
                    throw new InvalidOperationException("Can't find the directory for the VS Install");
                string baseDir = devenv.Substring(0, devenv.IndexOf("10.0\\") + 5);
                return "call \"" + baseDir + "VC\\vcvarsall.bat\"";
            }
            else
            {
                return base.TranslateMacro(input_string);
            }
        }
    }
}
